library FuelSynth initializer init requires LegacySystem, AmmoAPI

	private function FuelSynthesizerConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A031'
	endfunction

	private function FuelSynthesizer takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer fuel = 0
		
		if GetPrimaryAmmoRounds(p)==0 then
			call UnitAddAbility(u,'A044') //Flametip
		endif
		set udg_Reloading[GetConvertedPlayerId(p)] = 0
		call UnitRemoveAbility(u,'A02H')
		if GetPrimaryAmmoRounds(p)<2450 then
			set fuel = 50
		elseif GetPrimaryAmmoRounds(p) <2500 then
			set fuel = 2500 - GetPrimaryAmmoRounds(p)
		endif
		set udg_mystring = "+" + I2S(fuel)
		call AddPrimaryAmmo(u, 0, fuel)
		
		call AttachSoundToUnit(gg_snd_FuelSynthesizer, u)
		call SetSoundVolume(gg_snd_FuelSynthesizer, 127)
		call StartSound(gg_snd_FuelSynthesizer)
		call AddFadingTextTag(udg_mystring,GetUnitX(u),GetUnitY(u),255,220,0,200)
		set u = null
	endfunction
	
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition( t, Condition( function FuelSynthesizerConditions ) )
		call TriggerAddAction( t, function FuelSynthesizer )
	endfunction

endlibrary